package cn.com.blueInfo.core.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author suxch
 * @since 2025/9/1 23:45
 */
@Configuration
public class JacksonConfig {

    // 定义日期格式
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        ObjectMapper objectMapper = new ObjectMapper();

        // 1. 配置日期序列化格式（原日期配置保留）
        JavaTimeModule module = new JavaTimeModule();
        // 配置 LocalDate 序列化格式
        module.addSerializer(LocalDate.class,
                new LocalDateSerializer(DateTimeFormatter.ofPattern(DATE_FORMAT)));
        // 配置 LocalDateTime 序列化格式（去除 T）
        module.addSerializer(LocalDateTime.class,
                new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DATE_TIME_FORMAT)));

        // 2. 配置null转空字符串
        objectMapper.setSerializerFactory(objectMapper.getSerializerFactory()
                .withSerializerModifier(new BeanSerializerModifier() {
                    @Override
                    public List<BeanPropertyWriter> changeProperties(
                            SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
                        // 遍历所有字段，对字符串类型的null值进行处理
                        for (BeanPropertyWriter writer : beanProperties) {
                            // 判断字段类型是否为String
                            if (writer.getType().getRawClass().equals(String.class)) {
                                // 自定义序列化器：null时输出空字符串
                                writer.assignSerializer(new JsonSerializer<Object>() {
                                    @Override
                                    public void serialize(Object value, JsonGenerator gen,
                                                          SerializerProvider serializers) throws IOException {
                                        if (value == null) {
                                            gen.writeString(""); // null转空字符串
                                        } else {
                                            gen.writeString(value.toString()); // 非null正常输出
                                        }
                                    }
                                });
                            }
                        }
                        return beanProperties;
                    }
                }));

        objectMapper.registerModule(module);
        return new MappingJackson2HttpMessageConverter(objectMapper);
    }

}
